<?php
namespace Modules\Config\Controllers;

use Core\Mvc\Controller;

class AdminController extends Controller
{
    public function configListAction($contentModel)
    {
        $configListContentModelList = $this->entityManager->contentModel('configList');
        if (!isset($configListContentModelList[$contentModel])) {
            throw new \Exception('实体configList中没有内容类型：' . $contentModel);
        }
        $contentModelInfo = $configListContentModelList[$contentModel];
        $entity = $this->entityManager->get('configList');
        if ($entity->setField($contentModel) !== false) {
            $entityForm = $entity->addForm();
            if ($entityForm->hasSubmit()) {
                if ($entity->getSaveState() === true) {
                    $this->flash->success('保存成功');
                } elseif ($entity->getSaveState() === false) {
                    $this->flash->error('保存失败');
                } else {
                    $this->flash->error('验证失败');
                }
            }
        } else {
            $entityForm = false;
        }
        $content = [];
        $data = $this->entityManager->find('configList', ['contentModel' => $contentModel]);

        self::$variables['title'] = $contentModelInfo['name'];
        self::$variables['description'] = $contentModelInfo['description'];
        self::$variables += array(
            '#templates' => 'page',
            'breadcrumb' => array(
                'module' => array(
                    'name' => $contentModelInfo['name'],
                ),
            ),
            'content' => &$content,
        );
        $content['ConfigList'] = array(
            '#templates' => 'box',
            'title' => $contentModelInfo['name'] . '列表',
            'max' => false,
            'color' => 'primary',
            'size' => '6',
            'wrapper' => true,
            'content' => array(
                'dataList' => array(
                    '#templates' => array(
                        'configListTable',
                        'configListTable-' . $contentModel,
                    ),
                    '#module' => $contentModelInfo['module'],
                    'theadDisplay' => true,
                    'thead' => [],
                    'checkAll' => false,
                    'data' => $data,
                ),
            ),
        );
        $content['termHandle'] = array(
            '#templates' => array(
                'box',
                'box-right',
            ),
            'id' => 'right_handle',
            'title' => '添加' . $contentModelInfo['name'],
            'wrapper' => true,
            'max' => true,
            'color' => 'success',
            'size' => '6',
            'content' => array(
                'termList' => $entityForm->renderForm(),
            ),
        );
    }

    public function configListEditAction($contentModel, $id)
    {
        $content = [];
        $contentModelList = $this->entityManager->contentModel('configList');
        if (!isset($contentModelList[$contentModel])) {
            return $this->notFount();
        }
        $contentModelInfo = $contentModelList[$contentModel];
        $entity = $this->entityManager->get('configList', ['id' => $id, 'contentModel' => $contentModel]);
        $entityForm = $entity->editForm();

        if ($entityForm->hasSubmit()) {
            if ($entity->getSaveState() === true) {
                $this->flashSession->success('保存成功');
            } elseif ($entity->getSaveState() === false) {
                $this->flashSession->error('保存失败');
            } else {
                $this->flashSession->error('验证失败');
            }
            return $this->temMoved([
                'for' => 'adminConfigList',
                'contentModel' => $contentModel
            ]);
        }
        self::$variables['title'] = '编辑 ' . $contentModelInfo['name'] . '<' . $entity->name . '>';
        self::$variables['description'] = $contentModelInfo['description'];
        self::$variables += array(
            'id' => $id,
            'contentModel' => $contentModel,
            '#templates' => 'pageNoWrapper',
            'breadcrumb' => array(
                'module' => array(
                    'name' => $contentModelInfo['name'],
                ),
            ),
            'content' => &$content,
        );

        $content['termHandle'] = array(
            '#templates' => array(
                'box',
                'box-right',
            ),
            'id' => 'right_handle',
            'title' => '编辑 ' . $contentModelInfo['name'],
            'wrapper' => false,
            'max' => true,
            'color' => 'success',
            'size' => '12',
            'content' => array(
                'termList' => $entityForm->renderForm(),
            ),
        );
    }

    public function configListDeleteAction($contentModel, $id)
    {
        $entity = $this->entityManager->get('configList', ['contentModel' => $contentModel, 'id' => $id]);

        if ($entity && $entity->delete()) {
            $this->flash->success('删除成功');
        } else {
            $this->flash->error('删除失败');
        }
        return $this->moved(array(
            'for' => 'adminConfigList',
            'contentModel' => $contentModel,
        ));
    }

    public function configAction()
    {
        $configData = $this->entityManager->find('config');
        $content = [];
        self::$variables['title'] = '配置';
        self::$variables['description'] = '';
        self::$variables += array(
            '#templates' => 'page',
            'breadcrumb' => array(
                'module' => array(
                    'name' => '配置',
                ),
            ),
            'content' => &$content,
        );
        $content['ConfigList'] = array(
            '#templates' => 'box',
            'title' => '列表',
            'max' => false,
            'color' => 'primary',
            'size' => '12',
            'wrapper' => true,
            'content' => array(
                'dataList' => array(
                    '#templates' => array(
                        'configTable',
                    ),
                    '#module' => 'config',
                    'theadDisplay' => true,
                    'thead' => [
                        'id' => '机读名',
                        'description' => '描述'
                    ],
                    'checkAll' => false,
                    'data' => $configData,
                ),
            ),
        );
    }

    public function configEditAction($configId)
    {
        $configData = $this->entityManager->find('config');
        $entity = $this->entityManager->get('config', ['id' => $configId]);
        $entityEditForm = $entity->editForm();
        $saveState = $entity->getSaveState();
        if ($saveState === false) {
            $this->flash->error('保存失败');
        } elseif ($saveState === true) {
            $this->flash->success('保存成功');
        }
        $content = [];
        self::$variables['title'] = $entity->name;
        self::$variables['description'] = $entity->description;
        self::$variables += array(
            '#templates' => 'page',
            'breadcrumb' => array(
                'configList' => array(
                    'href' => array(
                        'for' => 'adminConfig',
                    ),
                    'name' => '配置',
                ),
                'configEdit' => array(
                    'name' => $entity->name,
                ),
            ),
            'content' => &$content,
        );

        $content['configList'] = array(
            '#templates' => 'configTabs',
            'size' => '1',
            'data' => $configData
        );

        $content['configEdit'] = array(
            '#templates' => array(
                'box',
                'box-right',
            ),
            'id' => 'right_handle',
            'title' => '编辑 ' . $entity->name,
            'wrapper' => true,
            'max' => true,
            'color' => 'success',
            'size' => '11',
            'content' => array(
                'termList' => $entityEditForm->renderForm(),
            ),
        );
    }
}
